package langBundle;

import java.lang.reflect.Method;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.border.TitledBorder;

import windowUnits.ActionButtonPane;
import windowUnits.HistoryToolBar;

public class MsgControl {
	
	Object msgBundle = null;
	String aClass = "";
    String aMethod = "getMsgBundle";
	Class<?> params[] = {};
    Object paramsObj[] = {};
	
	public MsgControl(String lang) {
		
		aClass = "langBundle.messageBundle_"+lang.toLowerCase();
		
		try{
			Class.forName(aClass);
		}catch(Exception e){
			aClass = "langBundle.messageBundle_en";
		}
		
	}
	
	public void setMsgByName(JMenu tagName, String cName){
		String msg[][] = null;
		try {
			Class<?> thisClass = Class.forName(aClass);
		    Object iClass = thisClass.newInstance();
		    Method thisMethod = thisClass.getDeclaredMethod(aMethod, params);
		    msg = (String[][]) thisMethod.invoke(iClass, paramsObj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		tagName.setText(getMsgByName(msg,cName));
		
	}
	
	public void setMsgByName(JMenuItem tagName, String cName){
		String msg[][] = null;
		try {
			Class<?> thisClass = Class.forName(aClass);
		    Object iClass = thisClass.newInstance();
		    Method thisMethod = thisClass.getDeclaredMethod(aMethod, params);
		    msg = (String[][]) thisMethod.invoke(iClass, paramsObj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		tagName.setText(getMsgByName(msg,cName));
		
	}
	
	public void setMsgByName(ActionButtonPane tagName, String cName){
		String msg[][] = null;
		try {
			Class<?> thisClass = Class.forName(aClass);
		    Object iClass = thisClass.newInstance();
		    Method thisMethod = thisClass.getDeclaredMethod(aMethod, params);
		    msg = (String[][]) thisMethod.invoke(iClass, paramsObj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		tagName.getButton().setText(getMsgByName(msg,cName));
		
	}
	
	public void setMsgByName(HistoryToolBar tagName, String cName){
		String msg[][] = null;
		try {
			Class<?> thisClass = Class.forName(aClass);
		    Object iClass = thisClass.newInstance();
		    Method thisMethod = thisClass.getDeclaredMethod(aMethod, params);
		    msg = (String[][]) thisMethod.invoke(iClass, paramsObj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		tagName.setTitle(getMsgByName(msg,cName));
		
	}
	
	public void setMsgByName(TitledBorder tagName, String cName){
		String msg[][] = null;
		try {
			Class<?> thisClass = Class.forName(aClass);
		    Object iClass = thisClass.newInstance();
		    Method thisMethod = thisClass.getDeclaredMethod(aMethod, params);
		    msg = (String[][]) thisMethod.invoke(iClass, paramsObj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		tagName.setTitle(getMsgByName(msg,cName));
		
	}
	
	public void setMsgByName(JLabel tagName, String cName){
		String msg[][] = null;
		try {
			Class<?> thisClass = Class.forName(aClass);
		    Object iClass = thisClass.newInstance();
		    Method thisMethod = thisClass.getDeclaredMethod(aMethod, params);
		    msg = (String[][]) thisMethod.invoke(iClass, paramsObj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		tagName.setText(getMsgByName(msg,cName));
		
	}
	
	public void setMsgByName(JButton tagName, String cName){
		String msg[][] = null;
		try {
			Class<?> thisClass = Class.forName(aClass);
		    Object iClass = thisClass.newInstance();
		    Method thisMethod = thisClass.getDeclaredMethod(aMethod, params);
		    msg = (String[][]) thisMethod.invoke(iClass, paramsObj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		tagName.setText(getMsgByName(msg,cName));
		
	}
	public String getByName(String cName){
		String msg[][] = null;
		try {
			Class<?> thisClass = Class.forName(aClass);
		    Object iClass = thisClass.newInstance();
		    Method thisMethod = thisClass.getDeclaredMethod(aMethod, params);
		    msg = (String[][]) thisMethod.invoke(iClass, paramsObj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return getMsgByName(msg,cName);
	}
	private String getMsgByName(String msg[][], String tagName){
		String message = null;
		for(int i=0;i<msg.length;i++){
			if(tagName.equals(msg[i][0]))
				message = msg[i][1];
		}
		return message;
	}
	
}
